﻿using gbL.NetBoard.Examples.DoubleMVC.Controller.Utils;
using gbL.NetBoard.Examples.DoubleMVC.View.UI.Requests;

namespace gbL.NetBoard.Examples.DoubleMVC.Controller
{
    /// <summary>
    /// Controller which coordinates browsing through database records
    /// </summary>
    public class BrowseController
    {
        public Blackboard Domain { get; set; }
        public IInputSource GetKeyPress { get; set; }

        public BrowseController(Blackboard domain) : this(domain, new ConsoleInputSource()) { }

        public BrowseController(Blackboard domain, IInputSource getKeyPress)
        {
            Domain = domain;
            GetKeyPress = getKeyPress;
        }

        public ControllerDispatch Default()
        {
            DispatchOption[] options = new[] {
                new DispatchOption('P', "Products", () => new BrowseController(Domain).Products()),
                new DispatchOption('O', "Options", () => new BrowseController(Domain).Options()),
                new DispatchOption('X', "Exit", () => null)
            };
            return DisplayAndChoose("Browse menu", options);
        }

        public ControllerDispatch Products()
        {
            DispatchOption[] options = new[] {
                new DispatchOption('L', "List", () => new ProductsController(Domain).List()),
                new DispatchOption('M', "Main menu", () => new BrowseController(Domain).Default())
            };
            return DisplayAndChoose("Products menu", options);
        }

        private ControllerDispatch Options()
        {
            DispatchOption[] options = new[] {
                new DispatchOption('M', "Main menu", () => new BrowseController(Domain).Default())
            };
            return DisplayAndChoose("Options menu", options);
        }

        private ControllerDispatch DisplayAndChoose(string title, DispatchOption[] options)
        {
            MenuViewRequest request = MenuControllerHelpers.BuildMenu(title, options);
            Domain.Add(this, request); // display it
            return MenuControllerHelpers.ChooseControllerDispatch(options, GetKeyPress);
        }

    }
}
